<?php

include_once '../inc/db_util.php';

function find($filter) {
    $array_wiki = array();
    if (!is_null($filter)) {
        $jsArray = json_decode($filter, false);
        if ($jsArray->{'Id'} != NULL) {
            $query = mysql_query("SELECT * FROM tb_wiki tw 
				JOIN tb_kategori tk ON tk.id_kategori = tw.id_kategori
                LEFT JOIN tb_dosen td on tw.id_user=td.nip
                LEFT JOIN tb_mahasiswa tm on tw.id_user=tm.id_mahasiswa
                WHERE tw.id_kategori='" . $jsArray->{'Id'} . "' OR tw.id_wiki='" . $jsArray->{'Id'} . "'");
        } else {
            $query = mysql_query("SELECT * FROM tb_wiki tw 
				JOIN tb_kategori tk ON tk.id_kategori = tw.id_kategori
                LEFT JOIN tb_dosen td on tw.id_user=td.nip
                LEFT JOIN tb_mahasiswa tm on tw.id_user=tm.id_mahasiswa
                WHERE (isi LIKE '%" . $jsArray->{'Isi'} . "%' OR judul LIKE '%" . $jsArray->{'Judul'} . "%') 
					AND tk.id_kategori='" . $jsArray->{'Kategori'}->{'Id'} . "'");
        }
    } else {
        $query = mysql_query("SELECT * FROM tb_wiki tw JOIN tb_kategori tk ON tk.id_kategori = tw.id_kategori
                LEFT JOIN tb_dosen td on tw.id_user=td.nip
                LEFT JOIN tb_mahasiswa tm on tw.id_user=tm.id_mahasiswa");
    }
    while ($row = mysql_fetch_array($query, MYSQL_BOTH)) {
        $row_array['Id'] = $row['id_wiki'];
        $row_array['Judul'] = $row['judul'];
        $row_array['Isi'] = $row['isi'];
        $row_array['Created'] = $row['tanggal_create'];
        $row_array['Kategori']['Id'] = $row['id_kategori'];
        $row_array['Kategori']['Kategori'] = $row['kategori'];
        $row_array['Creator']['IdUser'] = $row['id_user'];
        $row_array['Creator']['Username'] = $row['nama'] == '' ? $row['namadosen'] : $row['nama'];
        $row_array['Creator']['Type'] = $row['nama'] == '' ? 'Dosen' : 'Mahasiswa';

        array_push($array_wiki, $row_array);
    }
    return json_encode($array_wiki);
}

function create($json) {
    $jsArray = json_decode($json, false);
    $sql = "INSERT INTO tb_wiki (id_user, id_kategori, judul, isi, tanggal_create)
        VALUES (
        '" . $jsArray->{'Creator'}->{'Id'} . "',
        '" . $jsArray->{'Kategori'}->{'Id'} . "',
        '" . $jsArray->{'Judul'} . "',
        '" . $jsArray->{'Isi'} . "',
        NOW())";

    return mysql_query($sql);
}

function update($json) {
    $jsArray = json_decode($json, false);
	$sqlLog = "INSERT INTO tb_log(id_user, id_wiki, isi_lama, tanggal_edit)
				SELECT " . $jsArray->{'Creator'}->{'Id'} . ", id_wiki, isi, NOW() 
				FROM tb_wiki WHERE id_wiki='" . $jsArray->{'Id'} . "'";
	if(mysql_query($sqlLog)){
		$sql = "UPDATE tb_wiki SET 
			isi='" . $jsArray->{'Isi'} . "'
			WHERE 
			id_wiki='" . $jsArray->{'Id'} . "'";

		return mysql_query($sql);
	} else {
		return false;
	}
}

function delete($json) {
    $jsArray = json_decode($json, false);
    $sql = "DELETE FROM tb_wiki WHERE id_wiki='" . $jsArray->{'Id'} . "'";

    return mysql_query($sql);
}

?>
